﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Linq;
using System.Windows;
namespace WPFExcelReport
{
    //
    public class YCAEKAUDITData : BaseFunction
    {
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        public string UserID
        {
            get
            {
                return app.User;
            }
        }
        SETINTEXDB INTEXDB = new SETINTEXDB();
        public YCAEKAUDITData()
            : base()
        {
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
        } 
        public DataSet LoadSupplier()
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dwData = INTEXDB.LIEFDAT.NewLIEFDATRow();
            dwData["LSMANDANT"] = "00";
            SResult = INTEXDBC.QueryLIEFDAT(dwData, 6);
            foreach (DataRow rx in SResult.Tables[0].Rows)
            {
                rx["LSNAME1"] = Helpers.Helper.GetGB2312Value(rx["LSNAME1"].ToString().Trim());
            }
            SReturn.Merge(SResult);
            dwData = INTEXDB.ICC_FUNCTIONS.NewRow();
            dwData["MANDANT"] = "00";
            SResult = INTEXDBC.QueryICC_FUNCTIONS(dwData, 9999);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                row["DESCRIPTION"] = Helpers.Helper.GetGB2312Value(row["DESCRIPTION"].ToString().Trim());
            }
            SReturn.Merge(SResult);
            dwData = INTEXDB.ICC_WFEVENTDEF.NewRow();
            dwData["MANDANT"] = "00";
            dwData["WFCLASS"] = "002";
            SResult = INTEXDBC.QueryICC_WFEVENTDEF(dwData, 2);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                row["WFEDES"] = Helpers.Helper.GetGB2312Value(row["WFEDES"].ToString().Trim());
            }
            SReturn.Merge(SResult);
            return SReturn;
        }
        public DataSet GetOrderBySupplier(DataRow dwInput)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dwData = INTEXDB.EKKOPF.NewRow();
            dwData["EKKMANDANT"] = "00";
            dwData["EKKLIEFNR"] =dwInput["LSLIEFNR"].ToString();
            SResult = INTEXDBC.QueryEKKOPF(dwData, 3);
            SReturn.Merge(SResult);
            return SReturn;
        }
        public DataSet BuildData(DataRow dwInput)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            DataRow rowtemp = null;
            try
            {
                rowtemp = INTEXDB.EKKOPF.NewRow();
                rowtemp["EKKMANDANT"] = dwInput["EKPMANDANT"];
                rowtemp["EKKAUFKRS"] = dwInput["EKPAUFKRS"];
                rowtemp["EKKAUFLFD"] = dwInput["EKPAUFLFD"];
                Result = INTEXDBC.QueryEKKOPF(rowtemp);
                Return.Merge(Result);               
                #region=======Order======
                foreach (DataRow row in Return.Tables["EKKOPF"].Rows)
                {
                    rowtemp = INTEXDB.EKKOPF.NewRow();
                    rowtemp["EKKMANDANT"] = row["EKKMANDANT"];
                    rowtemp["EKKAUFKRS"] = row["EKKAUFKRS"];
                    rowtemp["EKKAUFLFD"] = row["EKKAUFLFD"];
                    Result = INTEXDBC.QueryLIEFDAT(rowtemp, 5);
                    Return.Merge(Result); 
                    Result = INTEXDBC.QueryLIEFKO(rowtemp, 5);
                    Return.Merge(Result);
                    Result = INTEXDBC.QueryWAPROF(rowtemp, 5);
                    Return.Merge(Result);
                    rowtemp = INTEXDB.EKPOS.NewRow();
                    rowtemp["EKPMANDANT"] = row["EKKMANDANT"];
                    rowtemp["EKPAUFKRS"] = row["EKKAUFKRS"];
                    rowtemp["EKPAUFLFD"] = row["EKKAUFLFD"];
                    Result = INTEXDBC.QueryICC_EKPOS(rowtemp, 4);
                    Return.Merge(Result);

                    rowtemp = INTEXDB.ICC_ABAUFM.NewRow();
                    rowtemp["ABAMANDANT"] = row["EKKMANDANT"];
                    rowtemp["ABAAUFKRS"] = row["EKKAUFKRS"];
                    rowtemp["ABAAUFLFD"] = row["EKKAUFLFD"];
                    Result = INTEXDBC.QueryICC_ABAUFM(rowtemp, 4);
                    Return.Merge(Result);
                    rowtemp = INTEXDB.ICC_ABAUFM.NewRow();
                    rowtemp["ABAMANDANT"] = row["EKKMANDANT"];
                    rowtemp["ABAAUFKRS"] = row["EKKAUFKRS"];
                    rowtemp["ABAAUFLFD"] = row["EKKAUFLFD"];
                    Result = INTEXDBC.QueryICC_ABAUFM(rowtemp, 1);
                    if (Result.Tables.Count > 0)
                    {
                        Result.Tables[0].TableName = "ABPOS";
                        Return.Merge(Result);
                    }
                    rowtemp = INTEXDB.ICC_ABAUFM.NewRow();
                    rowtemp["ABAMANDANT"] = row["EKKMANDANT"];
                    rowtemp["ABAAUFKRS"] = row["EKKAUFKRS"];
                    rowtemp["ABAAUFLFD"] = row["EKKAUFLFD"];
                    Result = INTEXDBC.QueryICC_ABAUFM(rowtemp, 21);
                    if (Result.Tables.Count > 0)
                    {
                        Result.Tables[0].TableName = "MAINFABRIC";
                        Return.Merge(Result);
                    }
                }
                #endregion
                #region========material==========
                /*var query = Return.Tables["ICC_EKPOS"].AsEnumerable()
                        .Select(row => new
                        {
                            ARTG = row.Field<string>("EKPARTGR"),
                            ART = row.Field<string>("EKPART"),
                            VAR = row.Field<string>("EKPVAR"),
                            COLOR = row.Field<string>("EKPFARBE"),
                            SIZE = row.Field<string>("EKPGROE")
                        }).Distinct();*/
                var query = Return.Tables["ICC_EKPOS"].AsEnumerable()
        .Select(row => new
        {
            ARTG = row.Field<string>("EKPARTGR"),
            ART = row.Field<string>("EKPART"),
            VAR = row.Field<string>("EKPVAR") 
        }).Distinct();
                foreach (var row in query)
                {
                    switch (row.ARTG)
                    {
                        case "1":
                            break;
                        case "2":
                            break;
                        case "3":
                            rowtemp = INTEXDB.WASTAMM.NewRow();
                            rowtemp["WAMANDANT"] = "00";
                            rowtemp["WAART"] = row.ART;
                            rowtemp["WAVAR"] =Left(row.VAR,2);
                            Result = INTEXDBC.QueryWASTAMM(rowtemp, 2);
                            Return.Merge(Result);
                            rowtemp = INTEXDB.STUELI.NewRow();
                            rowtemp["STUELIMANDANT"] = "00";
                            rowtemp["STUELISAPD"] = row.ART;
                            Result = INTEXDBC.QuerySTUELI(rowtemp, 3);
                            Return.Merge(Result);
                            //FSSTAMM;FSFARBEN;FSSTUELI;FSGROE
                            rowtemp = INTEXDB.WAFARBEN.NewRow();
                            rowtemp["WAFMANDANT"] = "00";
                            rowtemp["WAFART"] = row.ART;
                            rowtemp["WAFVAR"] = Left(row.VAR, 2);
                            Result = INTEXDBC.QueryWAFARBEN(rowtemp, 2);
                            Return.Merge(Result);
                            break;
                        case "4":
                            break;
                        case "5":
                            rowtemp = INTEXDB.FSSTAMM.NewRow();
                            rowtemp["FSMANDANT"] = "00";
                            rowtemp["FSART"] = row.ART;
                            rowtemp["FSVAR"] = Left(row.VAR, 2);
                            Result = INTEXDBC.QueryFSSTAMM(rowtemp, 1);
                            Return.Merge(Result);
                            //FSSTAMM;FSFARBEN;FSSTUELI;FSGROE
                            rowtemp = INTEXDB.FSFARBEN.NewRow();
                            rowtemp["FSFMANDANT"] = "00";
                            rowtemp["FSFART"] = row.ART;
                            rowtemp["FSFVAR"] = Left(row.VAR, 2);
                            Result = INTEXDBC.QueryFSFARBEN(rowtemp, 2);
                            Return.Merge(Result);
                            rowtemp = INTEXDB.FSGROE.NewRow();
                            rowtemp["FSGMANDANT"] = "00";
                            rowtemp["FSGART"] = row.ART;
                            rowtemp["FSGVAR"] = row.VAR;
                            Result = INTEXDBC.QueryFSGROE(rowtemp, 2);
                            Return.Merge(Result);
                            break;
                        case "6":
                            break;
                        case "7":
                            break;
                        case "8":
                            rowtemp = INTEXDB.MSSTAMM.NewRow();
                            rowtemp["MSMANDANT"] = "00";
                            rowtemp["MSMAT"] = Left(row.ART, 14);
                            rowtemp["MSVAR"] = row.VAR;
                            Result = INTEXDBC.QueryMSSTAMM(rowtemp, 1);
                            Return.Merge(Result);
                            break;
                        default:
                            break;
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }

        public void Delete(string client, string krs, string lfd, string pos = "000")
        {
            try
            { 
                DataSet setResult = INTEXDBC.QueryEKPOS(client, krs, lfd);
                if (!setResult.Tables.Contains("EKPOS"))
                    return;
                int count = setResult.Tables["EKPOS"].Rows.Count;
                int cdel = 0;

                var query = from DataRow row in setResult.Tables["EKPOS"].AsEnumerable()
                            select row;
                if (pos == "000")
                {
                    foreach (DataRow row in query)
                    {
                        decimal EKPME3 = System.Convert.ToDecimal(row["EKPME3"].ToString());
                        string EKPBEKZ = row["EKPBEKZ"].ToString();
                        string EKPKNTRNRL = row["EKPKNTRNRL"].ToString();
                        if (EKPBEKZ.Trim() != "")
                        {
                            MessageBox.Show("采购单数量已经确认，不能删除!");
                            return;
                        }
                        if (EKPKNTRNRL.Trim() != "")
                        {
                            MessageBox.Show("采购单已经审核，不能删除!");
                            return;
                        }
                        if (EKPME3 > 0)
                        {
                            MessageBox.Show("该采购单已经入库，不能删除!");
                            return;
                        }
                    }
                }
                if (pos != "000")
                {
                    query = from DataRow row in setResult.Tables["EKPOS"].AsEnumerable()
                            where row["EKPAUFPOS"].ToString() == pos
                            select row;
                    foreach (DataRow row in query)
                    {
                        decimal EKPME3 = System.Convert.ToDecimal(row["EKPME3"].ToString());

                        if (EKPME3 > 0)
                        {
                            if (MessageBox.Show("需要继续单行删除，请确认", "该采购单已经入库，不能删除!", MessageBoxButton.YesNo) == MessageBoxResult.No)
                            {
                                return;
                            }
                        }
                    }
                }
                foreach (DataRow row in setResult.Tables["EKPOS"].Rows)
                {

                    string posh = row["EKPAUFPOS"].ToString();
                    if (pos != "000")
                    {
                        if (posh != pos)
                            continue;
                    }
                    decimal EKPME3 = System.Convert.ToDecimal(row["EKPME3"].ToString());
                    if (EKPME3 > 0)
                    {
                        MessageBox.Show("该采购单已经入库，不能删除!");
                        continue;
                    }
                    INTEXDBC.DeleteEKPOS(client, krs, lfd, posh);
                    cdel++;
                    INTEXDBC.UpdateICC_ABAUFM("EK", client, krs, lfd, posh);
                }
                if (cdel == count)
                {
                    INTEXDBC.DeleteEKKOPF(client, krs, lfd);
                }
                DataRow dwData = INTEXDB.EKKOPF.NewRow();
                dwData["EKKMANDANT"] = "00";
                dwData["EKKAUFKRS"] =krs;
                dwData["EKKAUFLFD"] = lfd;
                INTEXDBC.UpdateEKKOPF(dwData, 2);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override DataSet LogIn(string MANDANT, string UsingID, System.Security.SecureString sPassword)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult;
            DataTable tableLocal = null;
            OracleCommand or1;
            try
            {
                IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(sPassword);
                string Password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);
                UsingID = UsingID.Trim().ToUpper();
                Password = Password.Trim().ToUpper();
                DataRow dwTemp = INTEXDB.HRSTAMM.NewRow();
                dwTemp["HRMANDANT"] = MANDANT;
                dwTemp["HRBENID"] = Left(UsingID + "                    ", 8);
                dwTemp["HRDFAX"] =Left(Password+"                    ",20);
                SResult = INTEXDBC.QueryHRSTAMM(dwTemp, 1);
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return SReturn;
        }
    }
}